﻿using System.IO;
#if !WINDOWS_PHONE
using System.Reactive.Linq;
#endif
#if WINDOWS_PHONE
using Microsoft.Phone.Reactive;
#endif

namespace System.Net
{
	public static class WebRequestExtensions
	{
		public static IObservable<WebResponse> ToObservable(this WebRequest request)
		{
			if (request == null) throw new ArgumentNullException("request");

			var getResponse = Observable
				.FromAsyncPattern<WebResponse>(request.BeginGetResponse, request.EndGetResponse);

			return getResponse();
		}

		public static string GetContentAsString(this WebResponse response)
		{
			if (response == null) throw new ArgumentNullException("response");

			using (var stream = response.GetResponseStream())
			using (var reader = new StreamReader(stream, false))
				return reader.ReadToEnd();
		}
	}
}
